from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64

class RSAUtil:
    @staticmethod
    def decrypt_str(ciphertext, private_key):
        try:
            key = RSA.import_key(base64.b64decode(private_key))
            cipher = PKCS1_v1_5.new(key)
            decrypted = cipher.decrypt(base64.b64decode(ciphertext), None)
            return decrypted.decode('utf-8')
        except Exception as e:
            print(e)
            raise RuntimeError(e)

    @staticmethod
    def encrypt_str(plaintext, public_key):
        try:
            key = RSA.import_key(base64.b64decode(public_key))
            cipher = PKCS1_v1_5.new(key)
            encrypted = cipher.encrypt(plaintext.encode('utf-8'))
            return base64.b64encode(encrypted).decode('utf-8')
        except Exception as e:
            print(e)
            raise RuntimeError(e)
    
    @staticmethod
    def get_private_key():
        return """MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCL1i+fn62CHFUAEIlsuSU8DM4PhqjqiW4itBCne/MRfKIsDCdevuw1OHCV80ICUjd5ke+Rr7AvwO3gU88M6UjGvCrJK/krElYigJswehgjmRMJAay8tO78h2ABgwvoUfkPOP7IHJaxiUaZGGzifd2cFNNRgRN9H20HTvlzmIhgBOcSVEVmJugo2BjzOzU4/Lu10D/arOn9EeofXXpfEBAo799OTm7nqXzKMA9Egdl/8ZIPhXrbMl6CVg4syYV5t46T0W7frRw7L78b2O3inxeH2LchEjDHKDQTNiexJ/xFQVCh1iVgoIwaKBLC6LMTPh06gZVDURKJB9wnfyTJ+hpNAgMBAAECggEABykkryviGrOQtrwiDWs9uOF++9SNedUnyqcl4y25uL+FHnRQ380vE1qciVE3pB7JsHQErJUulINwqvgfti2MCIFCP6L803PQ7Vtglw7phYklLGTlj5REWLIl/G3VgkQQWPM2ONEd9mFtOBHEIaUIYCHA4H+Xm+SsFJ+6rmy1LxV9ZW5ddDeGRIgZLCIDDJtsaEuc18RbvFE2S98lha1LAdL1Una210Rw70IUH358VzsRf/jfsaFeZZGnF1n7PXw5eor5aEKguCioiTqTyRfPtege2KdcRrhUWP2+JuYhFvA6eFq3/87hHizI6/Yrg2AggYUUY6gA6eS1tjE1gqXTEQKBgQDoed+flyVnqsCX3jWYybpsUy5rBDzVZODcoVbvgEl8Xw5R7nR93+R1g2NYmDPnS5XiEKWtNNYewo6Pv2pmRMbuyKX6B41a0IsDMIG1vAqbdc7Xj91eLCfTVPqS0AiGIFJX5CWFOCs70rglG3h/nZe5hcR11ltOAWqSqRlvcGcrUwKBgQCZ/I4O4T0d7s7BJAkHo41LIo3pVA7IkRn4DQTpF1BxElnWMNv6Vov0YbnXz84cfGmq4bTOX8ueHSTP0a0JR0nMvbWukj9p8C7nGmXJuHXpLJjraHTT+O8OkE30VajEyP2CnPNryfR66oi2XpUpRcXTj7KaVb2UyResIgbWUHiP3wKBgQCQUA+UtzQeHW5/GA73cMrMMfrPrgrBgWThMTqRZHa5wRxXmgowlYrxtAU42wrlWxOJCUJ/uhvtbmMnMvEu2SUQ1/fItWV3aZvR+AudMET5anFjeUg3DHwQgWEnQAL6mBflvZfZEhwsf8uWJW5w8fhcz4A8kjuNue1Za6WBeypgRwKBgB4Ml9g1ggy2TmiIVK7F7suruY+/1Ia1MiEiwUOPRiZak2dl73eBrhwJeg+wQKN0b9Zl5zeioASB4W4gl6jI3ZDzsGGZroBI245Dq3ta4L+Y8Vp27t1ypYvtAxlcIewM4NO9Nw9gwLG/1N/pwyfjssAfOZY+hxliyJjRpw3pdC13AoGAETuOBJDCHiGqBAFL7L0ctGdynOKOC4aMxbCeWYLQWYr3FtMJ11rCib4XxfGN6k/dXAcF97ivZjs8xCSHHx4ruzAFJyF9dshpMMeZB6pWDa0NX/jB9zYMGEbFiGz6uBqCYeL028xh8Nn2tmoXl5hEK5/QuUM0XSRt9ELLjFYAm4s="""

    @staticmethod
    def get_public_key():
        """获取RSA公钥"""
        return """MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0dPEMUuou9S8aeyxraN3JmZo7VLjsoTLPGdFduQcgbVJRYJMRPbL3ju73afvqvNDURBkFDcfgkeJFfVoFugMz5aLL8Pa/kTH9ClmDYQarhf4JHo8p5XDFlcih6PEU8gKKo+XoqeAld/fOyg3GRQ6+XY5iJ+/xOsoDHuAaKhfsSY2p6Am3tt5YpYeQKFsN1da7ewb+Fp2MvVhxLKhxiazSs27x7XdFlq0KlfWXKm2RqMuR90S59547KFjEkioyF9bLh6mh8OdqTuATC2T6Ne2Q0GeNJP68jeptgzsrlCXdNisQ3Rgv/pimxfF+uSoqJLH+4PeLYwvfTUZo5QzjeMESQIDAQAB"""

    @staticmethod
    def encrypt(plaintext, public_key):
        """Alias for encrypt_str"""
        return RSAUtil.encrypt_str(plaintext, public_key)
        
    @staticmethod
    def decrypt(ciphertext, private_key):
        """Alias for decrypt_str"""
        return RSAUtil.decrypt_str(ciphertext, private_key)
